package com.gxa.controller.asset;

import com.github.pagehelper.PageInfo;
import com.gxa.common.utils.R;
import com.gxa.entity.asset.search.MaintenPlanSearchMap;
import com.gxa.entity.asset.MaintenancePlan;
import com.gxa.service.asset.MaintenancePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.gxa.common.utils.R.error;
import static com.gxa.common.utils.R.ok;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:37:51
 */
@Api(tags = "设备运维--->运维计划")
@RestController
@RequestMapping("/maintenance-plan")
public class MaintenancePlanController {

    @Autowired
    private MaintenancePlanService maintenancePlanService;


    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("运维计划列表接口")
    @GetMapping("/list")
    public R queryAll(MaintenPlanSearchMap map){
        PageInfo<MaintenancePlan> page = maintenancePlanService.queryByPage(map);
        return ok("successs").put("data",page).put("frequency",maintenancePlanService.getfrequency());
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("新增运维计划接口")
    @PostMapping("/add")
    public R add(@RequestBody MaintenancePlan device){
        boolean save = maintenancePlanService.save(device);
        if (save){

        return ok("add success");
        }
        return error();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("删除运维计划接口")
    @DeleteMapping("/del/{id}")
    public R del(@PathVariable("id") Integer id){
        boolean remove = maintenancePlanService.removeById(id);
        if(remove){
            return R.ok();
        }
        return error();
    }

    @CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
    @ApiOperation("编辑运维计划接口")
    @PutMapping("/edit")
    public R edit(@RequestBody MaintenancePlan device){
        boolean update = maintenancePlanService.updateById(device);
        if(update){
            return ok();
        }
        return error("edit error");
    }
}
